package com.jcfk.eam.domain.dto.mould;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jcfk.eam.domain.dto.main.EamPointCheckDetailDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckOrderDTO;
import com.jcfk.eam.domain.po.main.EamPointCheckDetail;
import com.jcfk.eam.domain.po.main.EamPointCheckOrder;
import com.jcfk.eam.domain.query.main.EamPointCheckOrderQuery;
import com.jcfk.eam.service.main.EamPointCheckDetailService;
import com.jcfk.eam.service.main.EamPointCheckOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 入库尺寸明细趋势服务
 *
 * @author yrl
 * @email
 * @date 2024-11-04
 */
@Service
public class SizeDetailTrendService {

    @Resource
    private EamPointCheckOrderService eamPointCheckOrderService;

    @Resource
    private EamPointCheckDetailService eamPointCheckDetailService;

    /**
     * 入库尺寸明细趋势
     * @param requestParam 查询条件
     */
    public Map<String,Object> sizeDetailTrend(EamPointCheckOrderQuery requestParam) {
        // 查询条件不能为空
        Assert.isTrue(null != requestParam.getAssetId(),"查询条件不能为空");
        // 查询到的所有主表数据
        List<EamPointCheckOrder> list = eamPointCheckOrderService.list(Wrappers.<EamPointCheckOrder>lambdaQuery()
                .eq(null != requestParam.getAssetId(), EamPointCheckOrder::getAssetId, requestParam.getAssetId())
                .and(StrUtil.isNotEmpty(requestParam.getWorkTimeStart()) && StrUtil.isNotEmpty(requestParam.getWorkTimeEnd()),
                        (t) -> t.ge(EamPointCheckOrder::getWorkTime, requestParam.getWorkTimeStart()).le(EamPointCheckOrder::getWorkTime, requestParam.getWorkTimeEnd())));
        // 未按照条件查找到数据 返回空
        if (CollUtil.isEmpty(list)) return MapUtil.empty();
        // 查询子表数据
        List<EamPointCheckDetail> details = eamPointCheckDetailService.list(Wrappers.<EamPointCheckDetail>lambdaQuery()
                .in(EamPointCheckDetail::getOrderId, list.stream().map(EamPointCheckOrder::getId).collect(Collectors.toList())));
        List<EamPointCheckOrderDTO> eamPointCheckOrderDTOS = convertList(list, EamPointCheckOrderDTO.class);
        // 组合主子表数据
        Map<String, List<EamPointCheckDetail>> detailByOrderMap = details.stream().collect(Collectors.groupingBy(EamPointCheckDetail::getOrderId));
        eamPointCheckOrderDTOS.forEach(item -> item.setDetails(convertList(detailByOrderMap.getOrDefault(item.getId(), ListUtil.empty()), EamPointCheckDetailDTO.class)));
        // 组合前端显示数据
        //构建表格列名 去重 按照detail 的seq字段排序
        List<String> title = eamPointCheckOrderDTOS.stream()
                .map(EamPointCheckOrderDTO::getDetails)
                .flatMap(Collection::stream)
                .collect(Collectors.toMap(EamPointCheckDetailDTO::getSeq, EamPointCheckDetailDTO::getSopItemName, (v1, v2) -> v1))
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByKey())
                .map(Map.Entry::getValue)
                .collect(Collectors.toList());
        //构建表格数据 并按照workTime排序
        List<Map<String, Object>> data = eamPointCheckOrderDTOS.stream()
                .map(item -> {
                    Map<String, Object> result = item.getDetails()
                            .stream()
                            .filter(detail -> StrUtil.isNotEmpty(detail.getSopItemValue()))
                            .collect(Collectors.toMap(EamPointCheckDetailDTO::getSopItemName, EamPointCheckDetailDTO::getSopItemValue));
                    result.put("workTime", item.getWorkTime());
                    return result;
                })
                .sorted(Comparator.comparing(item -> DateUtil.parse(item.getOrDefault("workTime", DateUtil.now()).toString())))
                .collect(Collectors.toList());

        return MapUtil.<String, Object>builder()
                .put("title",title)
                .put("data",data)
                .build();
    }
}
